package com.atguigu.udmp.stream.extractor.db;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.support.spring.PropertyPreFilters;
import com.atguigu.udmp.stream.common.constant.UdmpConst;
import com.atguigu.udmp.stream.extractor.UserEventExtractor;
import org.apache.commons.lang3.time.DateFormatUtils;

import java.util.Date;


/*{
  "database": "gmall_2023_new",
  "table": "order_info",
  "type": "update",
  "ts": 1698217943,
  "xid": 19069,
  "commit": true,
  "data": {
    "id": 54256,
    "consignee": "马媛",
    "consignee_tel": "13598968738",
    "total_amount": 13927.00,
    "order_status": "1002",
    "user_id": 69,
    "payment_way": "3501",
    "delivery_address": null,
    "order_comment": null,
    "out_trade_no": "896113614523139",
    "trade_body": "索芙特i-Softto 口红不掉色唇膏保湿滋润 璀璨金钻哑光唇膏 Z03女王红 性感冷艳 璀璨金钻哑光唇膏 等4件商品",
    "create_time": "2023-10-31 15:12:22",
    "operate_time": "2023-10-31 15:12:23",
    "expire_time": null,
    "process_status": null,
    "tracking_no": null,
    "parent_order_id": null,
    "img_url": null,
    "province_id": 34,
    "activity_reduce_amount": 500.00,
    "coupon_reduce_amount": 0.00,
    "original_total_amount": 14427.00,
    "feight_fee": null,
    "feight_fee_reduce": null,
    "refundable_time": "2023-11-07 15:12:22"
  },
  "old": {
    "order_status": "1001",
    "operate_time": null
  }
}*/
public class OrderPayUserEventExtractor extends UserEventExtractor {
    @Override
    public boolean match(JSONObject dataJsonObj) {
        if( dataJsonObj.getString("table").equals("order_detail")
        && dataJsonObj.getJSONObject("data").getString("order_status").equals(UdmpConst.GMALL_ORDER_STATUS_PAID)){
            return true;
        }
        return false;
    }

    @Override
    public String extractDt(JSONObject dataJsonObj) {
        String operateTime = dataJsonObj.getJSONObject("data").getString("operate_time");
        String dt = operateTime.substring(0,10).replace("-","");
        return dt;
    }

    @Override
    public String extractEventTime(JSONObject dataJsonObj) {
        Date operateTime = dataJsonObj.getJSONObject("data").getDate("operate_time");
        String eventTime = DateFormatUtils.format(operateTime, "yyyy-MM-dd HH:mm:ss");
        return  eventTime;

    }

    @Override
    public Long extractUserId(JSONObject dataJsonObj) {
        return dataJsonObj.getJSONObject("data").getLong("user_id");
    }

    @Override
    public String extractEventInfoJson(JSONObject dataJsonObj) {
        PropertyPreFilters.MySimplePropertyPreFilter preFilter = new PropertyPreFilters().addFilter("order_status", "order_price", "sku_id", "split_total_amount", "sku_num", "create_time", "operate_time");
        String infoJson = JSON.toJSONString(dataJsonObj.getJSONObject("data"), preFilter);
        return infoJson;
    }


    //id
    @Override
    public String extractEventRecordKey(JSONObject dataJsonObj) {
        String id = dataJsonObj.getJSONObject("data").getString("id") ;
        return id;
    }
}
